package fr.sunnydale.android.geek.background.impl;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import fr.sunnydale.android.geek.background.IBackground;
import fr.sunnydale.android.geek.thread.TimerThread;
import fr.sunnydale.android.geek.util.Direction;
import fr.sunnydale.android.geek.util.GUtils;
import fr.sunnydale.android.geek.util.Speed;

public class Background implements IBackground {

    public static IBackground mInstance = new Background();
    public Bitmap mBitmap;
    public Speed mSpeed;
    public Direction mDirection;
    private float mCurrentPosition;
    private Paint mPaint = new Paint();
    private float scale = GUtils.SCALE;
    private float mRollingThreshold;

    private Background() {
    }

    @Override
    public void init(Bitmap bitmap, Speed speed, Direction direction) {
        mBitmap = bitmap;
        mSpeed = speed;
        mDirection = direction;
        mCurrentPosition = 0;
        mRollingThreshold = bitmap.getWidth();
    }

    @Override
    public void draw(Canvas canvas, TimerThread clock) {
        if (clock.hasToRefresh(mSpeed)) {
            mCurrentPosition += 1 * scale;
            if (mCurrentPosition > mRollingThreshold * 2) {
                mCurrentPosition = 0;
            }
            canvas.drawBitmap(mBitmap, mCurrentPosition - mRollingThreshold, 0, mPaint);
            canvas.drawBitmap(mBitmap, mCurrentPosition < mRollingThreshold ? mCurrentPosition : mCurrentPosition
                    - mRollingThreshold * 2, 0, mPaint);
        }
    }
}
